//
//  WGBShareSDKTool.m
//  RongNiuOnline
//
//  Created by 王贵彬 on 2018/5/1.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "WGBShareSDKTool.h"
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
#import <ShareSDKUI/SSUIShareActionSheetItem.h>


@implementation WGBShareSDKTool

static WGBShareSDKTool *_shareTool = nil ;
+ (WGBShareSDKTool *)shareInstance{
	if (!_shareTool) {
		static dispatch_once_t onceToken;
		dispatch_once(&onceToken, ^{
			_shareTool  =  [[WGBShareSDKTool alloc] init];
		});
	}
	return _shareTool;
}

/*
 * 添加自定义分享按钮
 **/
- (SSUIShareActionSheetCustomItem *)addCustomShareActionSheetItemWithTitle:(NSString *)title		icon:(UIImage *)img
																	 actionBlock:(dispatch_block_t)onClickCallBack{
		SSUIShareActionSheetCustomItem *item = [SSUIShareActionSheetCustomItem itemWithIcon: img label: title onClick:^{
			!onClickCallBack? : onClickCallBack();
		}];
	return item;
}

- (NSMutableDictionary *)shareParamsDict{
	WGBShareSDKTool *shareTool = [WGBShareSDKTool shareInstance];
		NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
		[shareParams SSDKSetupShareParamsByText: shareTool.desc
																		 images: @[shareTool.img]
																				url:  [NSURL URLWithString: shareTool.link]
																			title: shareTool.title
																			 type:SSDKContentTypeAuto];
			//客户端分享
		[shareParams SSDKEnableUseClientShare];
	return shareParams;
}

- (NSArray *)platforms{
	@weakify(self);
	SSUIShareActionSheetCustomItem *item1 = [self addCustomShareActionSheetItemWithTitle:@"二维码" icon: [UIImage imageNamed:@"rnol_home_icon004"] actionBlock:^{
		@strongify(self);
		!self.qrCodeBlock? : self.qrCodeBlock();
	}];
	SSUIShareActionSheetCustomItem *item2 = [self addCustomShareActionSheetItemWithTitle:@"推荐码" icon: [UIImage imageNamed:@"rnol_home_icon003"] actionBlock:^{
		@strongify(self);
		!self.referralCodeBlock? : self.referralCodeBlock();
//		[self addShareWithPlatform: SSDKPlatformSubTypeWechatSession];
	}];
	return @[ @(SSDKPlatformSubTypeWechatSession),
						@(SSDKPlatformSubTypeQQFriend),
						@(SSDKPlatformSubTypeQZone),
						@(SSDKPlatformTypeSinaWeibo),
						@(SSDKPlatformSubTypeWechatTimeline),
						@(SSDKPlatformTypeSMS),
						item1,
						item2];
}

- (void)showShareActionSheet{
			//分享
	 //将要自定义顺序的平台传入items参数中
		SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil  items:  [self platforms]    shareParams: [self shareParamsDict]  onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
				switch (state) {
					case SSDKResponseStateSuccess:
						{
					[kRootVC customAlertWithTitle: @"分享成功!" cancelAction: nil comfirmAction: nil];
					break;
						}
					case SSDKResponseStateFail:
						{
					NSLog(@"%@",kWGB_STR(@"分享失败%@",error.description));
					break;
						}
					default:
						break;
				}
		}];
		[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
		[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
}

	// 根据指定平台类型添加分享事件
- (void)addShareWithPlatform:(SSDKPlatformType)platformType{
	[ShareSDK share: platformType parameters: [self shareParamsDict] onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
		switch (state) {
			case SSDKResponseStateSuccess:
				{
			[kRootVC customAlertWithTitle: @"分享成功!" cancelAction: nil comfirmAction: nil];
			break;
				}
			case SSDKResponseStateFail:
				{
			NSLog(@"%@",kWGB_STR(@"分享失败%@",error.description));
			[kRootVC customAlertWithTitle: kWGB_STR(@"分享失败%@",error.description) cancelAction: nil comfirmAction: nil];
			break;
				}
			default:
				break;
		}
	}];
}
@end
